﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module;
using EMPower.Utility;
using Admin;
using BLL.Common;

public partial class Policy_PolicyAdd : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.SubjectTextBox.Focus();
            this.UpdateButton.Visible = false;
            this.SubmitButton.Visible = true;
            try
            {
                string param = Request.QueryString["u"];
                if (!string.IsNullOrWhiteSpace(param))
                {
                    try
                    {
                        param = Contexts.GetDecrypt(param);
                    }
                    catch (Exception ex)
                    {
                    }

                    long paperId = Convert.ToInt64(param);
                    PaperInfo paperInfo = BLL.BPaper.getInstance().GetPaperInfo(paperId);
                    this.SubjectTextBox.Text = paperInfo.Subject;
                    //this.BodyTextBox.Text = paperInfo.Body;
                    this.content.Value = paperInfo.Body;
                    this.UpdateButton.Visible = true;
                    this.SubmitButton.Visible = false;
                    this.paperIdHidden.Value = param;
                }
            }
            catch (Exception ex)
            {
            }
        }
    }

    /// <summary>
    ///  添加政策
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void SubmitButton_Click(object sender, EventArgs e)
    {
        PaperInfo paper = new PaperInfo();
        long paperId = IdHelper.GetLongId();
        paper.PaperId = paperId;
        paper.UserPassportId = AuthHelper.CurrentUserPassportId;
        paper.Subject = this.SubjectTextBox.Text;
        paper.Body = this.content.Value;// this.BodyTextBox.Text;
        paper.Type = 1;
        paper.TotalView = 0;
        paper.TotalReplay = 0;
        paper.IsTop = 0;
        paper.isFamousRegion = 0;
        paper.IsAllowComment = 1;
        paper.Status = 1;
        paper.LastUpdate = DateTime.Now;
        paper.DateCreated = DateTime.Now;
        BLL.BPaper.getInstance().Insert(paper);

        // 添加
        long policyPaperId = IdHelper.GetLongId();
        PolicyPaperInfo policyPaperInfo = new PolicyPaperInfo();
        policyPaperInfo.PolicyPaperId = policyPaperId;
        policyPaperInfo.PaperId = paperId;
        BLL.BPolicyPaper.getInstance().Insert(policyPaperInfo);
        Response.Redirect("PolicyList.aspx");
    }
    protected void UpdateButton_Click(object sender, EventArgs e)
    {
        string paperIdHidden = this.paperIdHidden.Value;
        long paperId = Convert.ToInt64(paperIdHidden);
        PaperInfo paperInfo = BLL.BPaper.getInstance().GetPaperInfo(paperId);
        paperInfo.Subject = this.SubjectTextBox.Text;
        paperInfo.Body = this.content.Value;// this.BodyTextBox.Text;
        paperInfo.LastUpdate = DateTime.Now;

        BLL.BPaper.getInstance().Update(paperInfo);
        this.paperIdHidden.Value = "";
        Response.Redirect("PolicyList.aspx");
    }
}